#!/usr/bin/env bash
# https://github.com/jasongin/nvs
i_nvs() {
  # set -e
  export NVS_HOME="$HOME/.nvs"
  if [ ! -d "$NVS_HOME" ]; then
    git clone https://github.com/jasongin/nvs "$NVS_HOME" --depth 1
  fi
  # 默认使用taobao源
  if [ "$(uname)" == "Darwin" ]; then
    echo "当前系统是 Mac"
    sed -i '' 's|"node": "https://nodejs.org/dist/"|"node": "https://cdn.npmmirror.com/binaries/node/"|g' "$NVS_HOME/defaults.json"
  else
    echo "当前系统不是 Mac"
    sed -i 's#"node": "https://nodejs.org/dist/"#"node": "https://cdn.npmmirror.com/binaries/node/"#g' "$NVS_HOME/defaults.json"
  fi
  /bin/zsh -c "./initNode.sh"
}

p_nvs() {
  NVS_HOME="$HOME/.nvs"
  cat <<EOM

# nvs config https://github.com/jasongin/nvs
export NVS_HOME="$HOME/.nvs"
[ -s "$NVS_HOME/nvs.sh" ] && . "$NVS_HOME/nvs.sh"

# alias
[ -s "$HOME/sh/alias.sh" ] && . "$HOME/sh/alias.sh"
EOM
}

if [ "$1" == "i" ]; then
  i_nvs
fi
if [ "$1" == "p" ]; then
  p_nvs >>"$HOME/.bashrc"
fi
